﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Useful;

namespace FileDuplicateRemover.Gui
{
    public class GridItemBindingList : SortableBindingList<GridItem>
    {
        public GridItemBindingList(IList<GridItem> list)
            : base(list)
        {
        }

        protected override void ApplySortCore(System.ComponentModel.PropertyDescriptor prop, System.ComponentModel.ListSortDirection direction)
        {
            //base.ApplySortCore(prop, direction);

            _sortProperty = prop;
            _sortDirection = direction;

            if (_sortProperty == null) return; //nothing to sort on
            List<GridItem> list = Items as List<GridItem>;
            if (list == null) return;

            GridItemSortComparer gridItemSortComparer = null;

            switch (prop.Name)
            {
                case "Length":
                    gridItemSortComparer = new GridItemSortComparerByLength(direction);
                    break;
                case "Count":
                    gridItemSortComparer = new GridItemSortComparerByCount(direction);
                    break;
                case "Name":
                    gridItemSortComparer = new GridItemSortComparerByName(direction);
                    break;
            }

            if (gridItemSortComparer != null)
                list.Sort(gridItemSortComparer);
            else
                throw new Exception("Сортировка по этому полю не поддерживается");

            _isSorted = true;
            //fire an event that the list has been changed.
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
    }
}
